#!/bin/bash
# linux-hdr-creator.sh
# Version 2.0.alpha (February 29 2012)
# Author: Diego Cirujano <dicir00@gmail.com>

# Site: https://code.google.com/p/linux-hdr-creator/
# This script is released under a GNU GPL License

# A bash script to create an HDR image out of set of JPG, NEF(not tested), CR2 or ORF images(much better) or just one RAW file
# Results in a Gimp xcf file containing 3 layers
#	an exposure blend of the original images using enfuse
#	a tone-mapped image using the mantiuk06 operator 50%
#	a tone-mapped image using the fattal02 operator 50%

# This script is based on the work of Edu Pérez - http://photoblog.edu-perez.com and Vincent Tassy - http://linuxdarkroom.tassy.net

# Version 2.0.alpha

# First version, just tested with a set of JPG, NEF(not tested), ORF and CR2 photos on Ubuntu. The previous version of Vincent needed pfscalibration libraries. Remaining task:
# - Check if the jpg is available without pfs calibration. 						Done, it works!
# - If pfscalibration tools available use the old version. 						Done, perfect!								
# - Organize all in methods.										Done, search new improvements. 
# - If there is no directory argument, show kdialog-select directory dialog.				Done.
# - By defaut, show kde gui(-k to disable it). Just when the command -o is added use console output.	Done.
# - Check all dependencies at first									Done, just gimp, ufraw and pfstools and enfuse What else?
# - If any of the libraries is not installed, suggest installation.					Postponed to ver 2.1
# - If there is just one photo RAW, make developments with 3 exposures.					Postponed to ver 2.1
# - If there is a -t argument, keeps the files generated. 						Done, easy.
# - Test NEF photo.											
# - Add RW2 file format(panasonic) support.								Done			
# - Test RW2 photo.											Done
# - Adjust values of the parameters									Done, removed gamma 2.2 from the fattal image creation.
# - Is it necessary pfscalibration for jpeg pictures?							Yes, so: in case of a set of jpegs and no pfscalibration installation, exit.
# - If there is a -p argument, it doesn't use pfscalibration and use aling instead			Done
# - The xcf file generated will have a name xxxxx_HDR.xcf						Done
# - Test on Fedora											


SELF=`basename $0`	# Ouselve
DIR=""
ALIGN=0			# Don't align the images by default
QUIET=1			# not too quiet by default
USEKDE=1		# command line invocation by default
GAMMA="0.45"		# Default gamma
LINEARITY="0.10"	# Default linearity
EXPOSURE="0.0"		# Default exposure compensation
SATURATION="1.0"	# Default saturation
CONFIGURATION=""
HDRFILEPREFIX="HDR"	# Prefix for the generated Gimp file
PFSCALIBRATION=1	# If the pfscalibration application is pressent, use it. Otherwise use align_image_stack to generate the hdr file.
REMOVETEMPORARYFILES=1	# If true, remove temporary files
ZENITY=0		# If Zenity is present, show zenity menu.
JUSTONEFILE=0		# When there is just one file

displayHelp() {
	echo "Create an HDR picture out of a set of bracketed images. The HDR generated is a 16-bit image."
	echo "Based on the work of Edu Pérez - http://photoblog.edu-perez.com and Vincent Tassy - http://linuxdarkroom.tassy.net"
	echo "Author: Diego Cirujano - http://l1br3n.blogspot.com/"
	echo
	echo "Usage: $SELF [OPTION] DIR"
	echo -e "  -a\t\tForce align the pictures first (in case of pfscalibration not present, used by default)"
	echo -e "  -g{val} \tgamma option for RAW conversion (--gamma={val} -- DEFAULT=0.45)"
	echo -e "  -s{val} \tsaturation option for RAW conversion (--saturation={val} -- DEFAULT=1.0)"
	echo -e "  -e{val} \texposure compensation option for RAW conversion (--exposure={val} -- DEFAULT=0.0)"
	echo -e "  -l{val} \tlinearity option for RAW conversion (--linearity={val} -- DEFAULT=0.10)"
	echo -e "  -c{path} \tConfiguration file for ufrraw IDFILE.ufraw"
	echo -e "  -o\t\tShow output"
	echo -e "  -p\t\tWithout pfscalibration"
	echo -e "  -k\t\tDisable GUI"
	echo -e "  -t\t\tKeep temporary files"
	echo -e "  -h\t\tThis help"
	echo
	echo -e "Example: ./linux-hdr-creator.sh -aopk /photos/hdr"
	echo "Report bugs to <dicir00@gmail.com> or https://code.google.com/p/linux-hdr-creator/"

}

checkKDialog() {
	#Check if kdialog is available
	if [ $USEKDE = 1 ]; then
		case $(kdialog --help|grep -c "getsavefilename") in 
			0)
				USEKDE=0
				echo "KDE libraries are not installed, command line output activated"
			;;
			*)
				USEKDE=1
			;;
		esac
	fi
}

setDirectory() {
	DIR=$1
	if [ -z $DIR ]; then
		if [ $USEKDE = 1 ]; then
			variable=`kdialog --getexistingdirectory *;`;
			if [ "$?" = 0 ]; then
				DIR=$variable
				if [ $QUIET = 0 ]; then
					echo "Folder selected: $DIR"
				fi
			elif [ "$?" = 1 ]; then
				displayHelp
				exit;
			else
				displayHelp
				exit;
			fi;
		else
			displayHelp
			exit;
		fi;
	fi

	if [ ! -d "$DIR" ]; then
		echo "$DIR is not a valid directory"
		displayHelp
		exit;
	fi
	DIR=$(cd "$DIR" && pwd) #transform to absolute path
}

checkGimp() {
	#Check if Gimp is present or not
	case $(man gimp | grep -c "gimprc") in 
		0)
			if [ $QUIET = 0 ]; then
				echo "Gimp is NOT installed."
			fi
			if [ $USEKDE = 1 ]; then
				kdialog --title createHDR --error "Gimp is NOT installed."
			fi
			exit;
		;;
		*) 
			if [ $QUIET = 0 ]; then
				echo "Gimp................installed."
			fi
		;;
	esac
}

checkUfraw() {
	#Check if ufraw is present or not
	case $(man ufraw-batch | grep -c "out-depth") in 
		0)
			if [ $QUIET = 0 ]; then
				echo "UFRaw is NOT installed."
			fi
			if [ $USEKDE = 1 ]; then
				kdialog --title createHDR --error "UFRaw is NOT installed."
			fi
			exit;
		;;
		*) 
			if [ $QUIET = 0 ]; then
				echo "UFRaw...............installed."
			fi
		;;
	esac
}

#enfuse -h | grep -c "Fuse INPUT images into a single IMAGE."
checkEnfuse() {
	#Check if enfuse is present or not
	case $(enfuse -h | grep -c "compression=COMPRESSION") in 
		0)
			if [ $QUIET = 0 ]; then
				echo "Enfuse is NOT installed."
			fi
			if [ $USEKDE = 1 ]; then
				kdialog --title createHDR --error "Enfuse is NOT installed."
			fi
			exit;
		;;
		*) 
			if [ $QUIET = 0 ]; then
				echo "Enfuse..............installed."
			fi
		;;
	esac
}

#man align_image_stack | grep -c "align_image_stack - Align overlapping images for HDR or focus stack"
checkAlign_image_stack() {
	#Check if align_image_stack is present or not
	case $(man align_image_stack | grep -c "Pablo d'Angelo") in 
		0)
			if [ $QUIET = 0 ]; then
				echo "align_image_stack is NOT installed."
			fi
			if [ $USEKDE = 1 ]; then
				kdialog --title createHDR --error "align_image_stack is NOT installed."
			fi
			exit;
		;;
		*) 
			if [ $QUIET = 0 ]; then
				echo "align_image_stack...installed."
			fi
		;;
	esac
}

#pfsin|grep -c "RAW"
checkPfstools() {
	#Check if align_image_stack is present or not
	case $(pfsin|grep -c "Radiance RGBE") in 
		0)
			if [ $QUIET = 0 ]; then
				echo "Pfstools is NOT installed."
			fi
			if [ $USEKDE = 1 ]; then
				kdialog --title createHDR --error "Pfstools is NOT installed."
			fi
			exit;
		;;
		*) 
			if [ $QUIET = 0 ]; then
				echo "Pfstools............installed."
			fi
		;;
	esac
}

checkCalibration() {
	#Check if pfscalibration is present or not
	case $(man pfshdrcalibrate | grep -c "pfsinhdrgen") in 
		0)
			if [ $QUIET = 0 ]; then
				echo "pfscalibration is NOT installed, forcing align_image_stack to generate the hdr file"
			fi
			PFSCALIBRATION=0
			ALIGN=1
		;;
		*) 
			if [ $QUIET = 0 ]; then
				echo "pfscalibration......installed, Pérez-Tassy available."
			fi
		;;
	esac
}

disableCalibration() {
			PFSCALIBRATION=0
			ALIGN=1
}

oneFileType() {
	# Check of the directory contains only one type of files
	if [ `find $DIR -maxdepth 1 -type f -exec basename {} \; | sed "s/.*\.//g" | tr '[:lower:]' '[:upper:]' | sort -u | wc -l` != 1 ]; then
		if [ $USEKDE = 1 ]; then
			kdialog --title createHDR --error "Directory contains multiple filetypes"
		else
			echo "Error: Directory contains multiple filetypes"
		fi
		exit;
	fi
}

#checks the number of files
oneFile() {
	if [ $(ls -l $DIR | grep ^- | wc -l) = 1 ]; then
	#	JUSTONEFILE=1
		echo "There is just one file"
		echo "    -\"The End\"-"
		exit;
	fi
}

deployCR2() {
	if [ $QUIET = 0 ]; then
		echo "Developing RAW files"
	fi
	if [ -z $CONFIGURATION ]; then
		LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --linearity=$LINEARITY --exposure=$EXPOSURE --saturation=$SATURATION --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
	else
		LC_ALL=C; ufraw-batch --conf=$CONFIGURATION --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
	fi
	# Also Generate a JPEG of the first image so as to save the EXIF metadata and embed it in the generated XCF
	LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --linearity=$LINEARITY --exposure=$EXPOSURE --saturation=$SATURATION --out-type=jpeg --compression=97 --overwrite --output=${FILES[0]%.*}.jpg ${FILES[0]} 2>/dev/null
	FILES=("$DIR"/*.tif)
: <<COMMENTBLOCK
	if [ $QUIET = 0 ]; then
		echo "Developing RAW files"
	fi
	if [ -z $CONFIGURATION ]; then
		if [ $JUSTONEFILE = 0 ]; then
			LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --linearity=$LINEARITY --exposure=$EXPOSURE --saturation=$SATURATION --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
		else
			LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --linearity=$LINEARITY --exposure=$EXPOSURE --saturation=$SATURATION --output="$DIR"/normalExposure.tif --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
			tmp=$(echo "$EXPOSURE+1" | bc)
			LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --linearity=$LINEARITY --exposure=$tmp --saturation=$SATURATION --output="$DIR"/plusExposure.tif --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
			tmp=$(echo "$EXPOSURE-1" | bc)
			LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --linearity=$LINEARITY --exposure=$tmp --saturation=$SATURATION --output="$DIR"/minusExposure.tif --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
		fi
	else
		LC_ALL=C; ufraw-batch --conf=$CONFIGURATION --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
	fi
	# Also Generate a JPEG of the first image so as to save the EXIF metadata and embed it in the generated XCF
	LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --linearity=$LINEARITY --exposure=$EXPOSURE --saturation=$SATURATION --out-type=jpeg --compression=97 --overwrite --output=${FILES[0]%.*}.jpg ${FILES[0]} 2>/dev/null
	FILES=("$DIR"/*.tif)
COMMENTBLOCK
}

deployRaw() {
	if [ $QUIET = 0 ]; then
		echo "Developing RAW files"
	fi
	if [ -z $CONFIGURATION ]; then
		LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --exposure=$EXPOSURE --saturation=$SATURATION --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
	else
		LC_ALL=C; ufraw-batch --conf=$CONFIGURATION --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
	fi
	# Also Generate a JPEG of the first image so as to save the EXIF metadata and embed it in the generated XCF
	LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --exposure=$EXPOSURE --saturation=$SATURATION --out-type=jpeg --compression=97 --overwrite --output=${FILES[0]%.*}.jpg ${FILES[0]} 2>/dev/null
	FILES=("$DIR"/*.tif)
: <<COMMENTBLOCK
	if [ $QUIET = 0 ]; then
		echo "Developing RAW files"
	fi
	if [ -z $CONFIGURATION ]; then
		if [ $JUSTONEFILE = 0 ]; then
			LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --exposure=$EXPOSURE --saturation=$SATURATION --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
		else
			LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --exposure=$EXPOSURE --saturation=$SATURATION --output="$DIR"/normalExposure.tif --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
			tmp=$(echo "$EXPOSURE+1" | bc)
			LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --exposure=$tmp --saturation=$SATURATION --output="$DIR"/plusExposure.tif --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
			tmp=$(echo "$EXPOSURE-1" | bc)
			LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --exposure=$tmp --saturation=$SATURATION --output="$DIR"/minusExposure.tif --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
		fi
	else
		LC_ALL=C; ufraw-batch --conf=$CONFIGURATION --out-type=tiff --out-depth=16 --overwrite ${FILES[*]} 2>/dev/null
	fi
	# Also Generate a JPEG of the first image so as to save the EXIF metadata and embed it in the generated XCF
	LC_ALL=C; ufraw-batch --wb=camera --gamma=$GAMMA --exposure=$EXPOSURE --saturation=$SATURATION --out-type=jpeg --compression=97 --overwrite --output=${FILES[0]%.*}.jpg ${FILES[0]} 2>/dev/null
	FILES=("$DIR"/*.tif)
COMMENTBLOCK
}

alignImages() {
	if [ $QUIET = 0 ]; then
		echo "Aligning images"
	fi
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef setLabelText "Aligning images"
	fi
	if [ $PFSCALIBRATION = 1 ]; then
			if [ $filetype = "JPG" ]; then
				align_image_stack -a "$DIR"/AIS_ ${FILES[*]} >/dev/null 2>&1
			else
				align_image_stack -a "$DIR"/AIS_ "$DIR"/*.tif >/dev/null 2>&1
			fi
	else
			if [ $filetype = "JPG" ]; then
				align_image_stack -a "$DIR"/AIS_ -o "$DIR"/output ${FILES[*]} >/dev/null 2>&1
			else
				align_image_stack -a "$DIR"/AIS_ -o "$DIR"/output "$DIR"/*.tif >/dev/null 2>&1
			fi
	fi
	FILES=("$DIR"/AIS_*.tif)
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef Set "" "value" 3
	fi
}

hdrGenerationWithPfsCalibration() {
	i=0
	cat "$DIR"/pfs.hdrgen | while read LINE; do
		echo "${FILES[$i]} $LINE" | cut -d' ' -f1,3- >> "$DIR"/pfs_updated.hdrgen # Keep meta-data but change files involved
		let "i = $i +1"
	done
	if [ $filetype = "JPG" ]; then
		pfsinhdrgen "$DIR"/pfs_updated.hdrgen | pfshdrcalibrate -c none -r gamma | pfsclamp --rgb | pfsout "$DIR"/output.hdr 2>/dev/null # Generate HDR image
	else
		pfsinhdrgen "$DIR"/pfs_updated.hdrgen | pfshdrcalibrate -c none -r gamma --bpp 16 | pfsclamp --rgb | pfsout "$DIR"/output.hdr 2>/dev/null # Generate HDR image
	fi
}

removeTemporaryFiles() {
	if [ $QUIET = 0 ]; then
		echo "Cleaning up"
	fi
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef setLabelText "Cleaning up"
	fi
	rm -f $DIR/*.tif $DIR/output.hdr $DIR/pfs.hdrgen $DIR/pfs_updated.hdrgen $DIR/*.jpg
	if [ $filetype != "JPG" ]; then
		rm -f $DIR/$JPEGFILENAME
	fi
}

# test params
while getopts aokhptg:l:e:s:c: argument
do
        case $argument in
                a)ALIGN=1;;
                o)QUIET=0;;
		k)USEKDE=0;;
                h)displayHelp;exit;;
		p)disableCalibration;;
		t)REMOVETEMPORARYFILES=0;;
                g)GAMMA=$OPTARG;;
		l)LINEARITY=$OPTARG;;
		e)EXPOSURE=$OPTARG;;
		s)SATURATION=$OPTARG;;
		c)CONFIGURATION=$OPTARG;;
        esac
done

shift $(($OPTIND-1))

checkKDialog

setDirectory $1

checkGimp
checkUfraw
checkEnfuse
checkAlign_image_stack
checkPfstools
checkCalibration

oneFileType

oneFile

# Check if the directory name ends with a number. If so, append it to the HDR file name
NUMBER=`echo $DIR | sed 's/^.*[^0-9]\([0-9]\+\)$/\1/'`
if [ $DIR = $NUMBER ]; then
	NUMBER=""	
fi

FILES=(`find $DIR -maxdepth 1 -type f -print | sort`) # List files in the selected directory

#HDRFILE="$HDRFILEPREFIX$NUMBER.xcf"
HDRFILE="${FILES[0]}_HDR.xcf"

filetype=`basename ${FILES[0]} | sed "s/.*\.//g" | tr '[:lower:]' '[:upper:]'` # Get file extension

if [ $filetype = "JPG" ] || [ $filetype = "CR2" ] || [ $filetype = "RW2" ] || [ $filetype = "NEF" ] || [ $filetype = "ORF" ]; then
	if [ $USEKDE = 1 ]; then
		dbusRef=`kdialog --title "createHDR" --progressbar "Parsing EXIF information" 9`
	fi
	if [ $QUIET = 0 ]; then
		echo "Files are $filetype"
		echo "Parsing EXIF information"
	fi
	if [ $filetype = "JPG" ]; then
		if [ $PFSCALIBRATION = 0 ]; then
			if [ $USEKDE = 1 ]; then
				kdialog --title createHDR --error "For jpeg hdr photos, pfscalibration needed."
			fi
			if [ $QUIET = 0 ]; then
				echo "For jpeg hdr photos, pfscalibration needed."
			fi
			exit;
		fi
		jpeg2hdrgen ${FILES[*]} > "$DIR"/pfs.hdrgen # Generate input file for pfstools
		JPEGFILENAME=${FILES[0]} # We'll use the first file to provide the EXIF info in the generated XCF
		if [ $USEKDE = 1 ]; then
			qdbus $dbusRef Set "" "value" 2
		fi
	else
		if [ $PFSCALIBRATION=1 ]; then
			dcraw2hdrgen ${FILES[*]} > "$DIR"/pfs.hdrgen # Generate input file for pfstools
		fi
		if [ $USEKDE = 1 ]; then
			qdbus $dbusRef Set "" "value" 1
			qdbus $dbusRef setLabelText "Developing RAW files"
		fi
		if [ $filetype = "CR2" ]; then
			deployCR2
		else
			deployRaw
		fi
		JPEGFILENAME=${FILES[0]%.*}.jpg # This is the name of the generated JPEG file to be used to preserve the EXIF metadata in the generated XCF
		if [ $USEKDE = 1 ]; then
			qdbus $dbusRef Set "" "value" 2
		fi
	fi
	if [ $ALIGN = 1 ]; then
		alignImages
	fi
	if [ $QUIET = 0 ]; then
		echo "Generating Enfused image"
	fi
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef setLabelText "Generating Enfused image"
	fi

	enfuse -o "$DIR"/enfuse.tif ${FILES[*]} >/dev/null 2>&1

	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef Set "" "value" 4
	fi
	if [ $QUIET = 0 ]; then
		echo "Generating HDR"
	fi
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef setLabelText "Generating HDR"
	fi
	
	#Creation of the hdr file with pfscalibration

	if [ $PFSCALIBRATION = 1 ] ; then
		hdrGenerationWithPfsCalibration
	fi

	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef Set "" "value" 5
	fi
	if [ $QUIET = 0 ]; then
		echo "Tone-mapping with mantiuk06 operator"
	fi
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef setLabelText "Tone-mapping with mantiuk06 operator"
	fi

	# Tonemap using mantiuk06
	pfsin "$DIR"/output.hdr | pfstmo_mantiuk06 -e 1 -s 1 2>/dev/null | pfsgamma --gamma 2.2 | pfsoutimgmagick "$DIR"/hdr_mantiuk06.tif >/dev/null 2>&1

	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef Set "" "value" 6
	fi
	if [ $QUIET = 0 ]; then
		echo "Tone-mapping with fattal02 operator"
	fi
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef setLabelText "Tone-mapping with fattal02 operator"
	fi

	# Tonemap using fattal02
	pfsin "$DIR"/output.hdr | pfstmo_fattal02 -s 1 | pfsoutimgmagick "$DIR"/hdr_fattal02.tif 

	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef Set "" "value" 7
	fi
	if [ $QUIET = 0 ]; then
		echo "Creating image stack"
	fi
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef setLabelText "Creating image stack"
	fi

	# Stack the generated images in Gimp
	gimp -c -d -i -f -s -n -b \
		'(define (create-hdr-stack jpegfilename enfusefilename mantiukfilename fattalfilename targetfilename)
		(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE jpegfilename jpegfilename)))
		(jpeglayer (car (gimp-image-get-active-layer image)))
		(enfuselayer (car (gimp-file-load-layer RUN-NONINTERACTIVE image enfusefilename)))
		(mantiuklayer (car (gimp-file-load-layer RUN-NONINTERACTIVE image mantiukfilename)))
		(fattallayer (car (gimp-file-load-layer RUN-NONINTERACTIVE image fattalfilename))))
		(gimp-image-add-layer image enfuselayer -1)
		(gimp-image-add-layer image mantiuklayer -1)
		(gimp-layer-set-mode mantiuklayer SOFTLIGHT-MODE)
		(gimp-layer-set-opacity mantiuklayer 50.0)
		(gimp-image-add-layer image fattallayer -1)
		(gimp-layer-set-mode fattallayer OVERLAY-MODE)
		(gimp-layer-set-opacity fattallayer 50.0)
		(gimp-image-remove-layer image jpeglayer)
		(gimp-xcf-save RUN-NONINTERACTIVE image fattallayer targetfilename targetfilename)
		(gimp-image-delete image)))

		(create-hdr-stack "'$JPEGFILENAME'" "'$DIR/enfuse.tif'" "'$DIR/hdr_mantiuk06.tif'" "'$DIR/hdr_fattal02.tif'" "'$HDRFILE'")
		(gimp-quit 0)' >/dev/null 2>&1
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef Set "" "value" 8
	fi
	if [ $REMOVETEMPORARYFILES = 1 ]; then
		removeTemporaryFiles
	fi
	if [ $USEKDE = 1 ]; then
		qdbus $dbusRef Set "" "value" 9
		qdbus $dbusRef close
	fi
else
	if [ $USEKDE = 1 ]; then
		kdialog --title createHDR --error "Unsupported file type: $filetype"
	else
		echo "Unsupported file type: $filetype"
	fi
fi
